﻿using BlogSystem.Data.Entity;
using BlogSystem.Model.Request;
using BlogSystem.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace BlogSystem.Controller;

/// <summary>
///  文章/帖子 控制器
/// </summary>
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class ArticleController :ControllerBase
{

    private readonly IArticleService _articleService;

    public ArticleController(IArticleService articleService)
    {
        _articleService = articleService;
    }
    
    //查看发布
    //[FromQuery] 属性用于从 HTTP 请求的查询字符串中绑定参数到控制器的 Action 方法。当你在控制器的 Action 方法中使用 [FromQuery] 时，
    //你可以指定默认值，这样即使客户端没有在查询字符串中提供该参数，你的 API 也能正常工作
    /// <summary>
    ///  获取文章列表
    /// </summary>
    /// <param name="keyWord">关键词</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">每页数据</param>
    /// <returns></returns>
    [HttpGet]
    public async Task<IActionResult> GetArticles([FromQuery] string? keyWord="",[FromQuery] int pageIndex=1,[FromQuery] int pageSize=10)
    {
        
        var articlevo = await _articleService.GetArticlesAsync(keyWord,pageIndex,pageSize);
        return articlevo!=null?Ok(new {code="200",msg="成功",total=articlevo.Count(),article=articlevo}):NoContent();

    }
    
    /// <summary>
    ///  添加
    /// </summary>
    /// <param name="getArticleRequest">请求体</param>
    /// <returns></returns>
    [HttpPost]
    public async Task<IActionResult> AddArticle(GetArticleRequest getArticleRequest)
    {
        
       var res = await _articleService.AddArticle(getArticleRequest);
        return res.Equals(true)?Ok(new { msg = "添加成功" }):NoContent();
    }

    [HttpPut]
    public async Task<IActionResult> updateArticle(int id,GetArticleRequest getArticleRequest)
    {
        var res = await _articleService.UpdateArticle(id,getArticleRequest);
        return res.Equals(true)?Ok(new { msg = "修改成功" }):NoContent();
    }
    
    // 根据id进行查找
    [HttpGet("{id}")]
    public async Task<IActionResult> GetById(int id)
    {
        var article =await _articleService.GetById(id);
        if (article == null)
            return NoContent();
        else
            return Ok(article);
    }
    
    //逻辑删除发布[通过帖子的id去修改status]
    [HttpDelete("{id}")]
    public async  Task<IActionResult> DelArticleById(int id)
    {
        bool res = await _articleService.DelArticleById(id);
        if(res)
            return Ok("删除成功。");
        else
            return BadRequest("无此数据。");
    }
    
    // 根据 帖子id 给对应的帖子点赞
    [HttpPost("{id}/likes")]
    public async Task<IActionResult>  AddLikeById(int id)
    {
        bool res = await _articleService.AddLikeById(id);
        return Ok("点赞成功。");
    }
    //收藏+1 
    [HttpPost("{id}/saves")]
    public async Task<IActionResult> AddSaveById(int id)
    {
        bool res =await _articleService.AddSaveById(id);
        return Ok("收藏成功。");
    }
    //阅读+1
    [HttpPost("{id}/reads")]
    public async Task<IActionResult> AddReadById(int id)
    {
        bool res =await _articleService.AddReadById(id);
        return Ok("阅读成功。");
    }
    

}